<?php

namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;
use think\Request;
use think\facade\Session;
use app\common\model\Admin;
use think\captcha\facade\Captcha;
use app\common\model\SiteSetting;

class AuthController
{

	/**
	 * 登录
	 * @return [type] [description]
	 */
	public function login(Request $request)
	{
        if(\request()->	isPost())
		{
            $username = $request->post('username');
            $password = $request->post('password');
            $captcha  = $request->post('captcha');
            if(!Captcha::check($captcha)){

                return returnFail('验证码不正确');
            }
            $admin_info = Admin::getAdminInfo(['username'=>$username]);

            if(!empty($admin_info))
            {
                if($admin_info['status'] != 1)
                {
                    return returnFail('您的账户已被禁止登录');
                }
                if(md5($password.$admin_info['salt']) == $admin_info['password'])
                {
                    unset($admin_info['salt']);
                    unset($admin_info['password']);

                    Session::set('admin_info', $admin_info);
                    Session::save();
                    return returnSuccess('登录成功');
                }else{
                    return returnFail('账号或密码错误');
                }
            }else{
                return returnFail('未获取到用户信息');
            }
		}else{
            //网站配置
            $site_info = SiteSetting::getSite();
            return View::fetch('login', ['site_info'=>$site_info]);
		}
	}


    /**
     * 退出登录
     * @params string
     * @return
     */
    public function logout(Request $request)
    {
        Session::delete('admin_info');
        return redirect('/admin/auth/login');
    }
}
